package com.example.bbs.handlerinterceptor;

import com.alibaba.fastjson.JSON;
import com.example.bbs.http.bean.HttpSendBasic;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Component
public class UserHandlerInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession();
        HttpSendBasic httpSendBasic = new HttpSendBasic();
        httpSendBasic.setSuccess(0);
        httpSendBasic.setMessage("请重新登陆");
        if (session.getAttribute("user") == null) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().println(JSON.toJSONString(httpSendBasic));
            return false;
        }
        return true;
    }
}
